//
//  MusicTitleStyleCell.swift
//  NDAi
//
//  Created by 丸子 on 2024/7/25.
//

import UIKit

class MusicTitleStyleCell: UITableViewCell, UITextFieldDelegate {
    var chooseTitlte:((String) -> Void)?
    var chooseMoveView:((Bool) -> Void)?

    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = Theme.color.main
        view.allRound(4)
        return view
    }()
    
    lazy var titleLb: UILabel = {
        let label = UILabel()
        label.font = UIFont.pingFangSCRegular(ofSize: 15)
        label.textColor = Theme.color.main
        let fullText = "歌曲名字*"
        let attributedString = NSMutableAttributedString(string: fullText)
          // 查找 * 的范围并设置其颜色为红色
        if let range = fullText.range(of: "*") {
            let nsRange = NSRange(range, in: fullText)
            attributedString.addAttribute(.foregroundColor, value: UIColor.red, range: nsRange)
        }
        label.attributedText = attributedString
        return label
    }()
    
    lazy var backView: UIView = {
       let view = UIView()
        view.backgroundColor = .white
        view.allRound(8)
        return view
    }()
    
    lazy var inputFiled: UITextField = {
        let textField = UITextField()
        textField.attributedPlaceholder = NSAttributedString.init(string: "请给歌曲取一个名字", attributes: [NSAttributedString.Key.font: UIFont.pingFangSCMedium(ofSize: 14), NSAttributedString.Key.foregroundColor: Theme.color.text_d3])
        textField.font = UIFont.pingFangSCMedium(ofSize: 14)
        textField.borderStyle = .none
        textField.textColor = Theme.color.main
        textField.addTarget(self, action: #selector(self.textFieldDidChange(_:)), for: .editingChanged)
        textField.delegate = self
        return textField
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = .clear
        selectionStyle = .none
        contentView.addSubviews([lineView, titleLb, backView])
        backView.addSubview(inputFiled)
        
        lineView.snp.makeConstraints { make in
            make.top.equalTo(16)
            make.leading.equalTo(16)
            make.height.equalTo(16)
            make.width.equalTo(6)
        }
        
        titleLb.snp.makeConstraints { make in
            make.centerY.equalTo(lineView)
            make.leading.equalTo(lineView.snp.trailing).offset(8)
        }
        
        backView.snp.makeConstraints { make in
            make.top.equalTo(titleLb.snp.bottom).offset(10)
            make.leading.trailing.equalToSuperview().inset(14)
            make.height.equalTo(50)
        }
    
        inputFiled.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(8)
        }
        
    }
    @objc func textFieldDidChange(_ textField: UITextField) {
        self.chooseTitlte?(textField.text ?? "")
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func textFieldDidBeginEditing(_ textField: UITextField) {
        self.chooseMoveView?(true)
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        self.chooseMoveView?(false)
    }
}
